import { defineStore } from 'pinia'
import {useMainStore} from "./main";
import {login} from "../api/user";
import router from "../router";
let mainStore = useMainStore();
export const useLoginStore = defineStore('login', {
    //TODO 待整理一下
    state: () => ({
        loginFrom:{
            //手机号
            phone:String(),
            //密码
            password:String(),
        },
        userInfo:localStorage.getItem("user")
    }),
    getters: {
        /**
         * 登录按钮在手机号和验证码都不为空时显示可以操作
         * @param state
         */
        loginButton: (state):boolean => {
            return !(state.loginFrom.phone !== "" && state.loginFrom.password !== "");
        },
        
        // /**
        //  * 
        //  * @param state 
        //  * @returns 
        //  */
        // getUserInfo:(state):Object=>{
        //   let user=localStorage.getItem("user")
        //   if(user){
        //     return JSON.parse(user)
        //   }
        //   return "";
        // }

    },
    actions: {
        handleLogin() {
               login(this.loginFrom).then(res=>{
                   if(res.data.code==200){
                       router.replace({
                           path:"/"
                       })
                   }
                localStorage.setItem("user",JSON.stringify(res.data.data))
                // localStorage.setItem("userId",userId)  
               })
        },
        getUserInfo(){ 
            if(this.userInfo!=null)
            return JSON.parse(this.userInfo)
        }

    }
})